package com.echo.register.cmn.controller;

import com.echo.register.cmn.service.DictService;
import com.echo.register.common.result.Result;
import com.echo.register.model.cmn.Dict;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @ClassName: DictController
 * @Package: com.echo.register.cmn.controller
 * @Description: 数据字典接口
 * @Author 谢同学
 * @Create 2023/12/14 14:51
 * @Version 1.0
 */
@Api(description = "数据字典接口")
@RestController
@RequestMapping("/admin/cmn/dict")
// @CrossOrigin
public class DictController {
    @Resource
    private DictService dictService;

    //根据数据id查询子数据列表
    @ApiOperation(value = "根据数据id查询子数据列表")
    @GetMapping("findChildData/{id}")
    public Result<List<Dict>> findChildData(@PathVariable Long id) {
        return Result.ok(dictService.findChildData(id));
    }

    /**
     * 导出数据
     * @param response 响应
     */
    @ApiOperation(value="导出")
    @GetMapping(value = "/exportData")
    public void exportData(HttpServletResponse response) {
        dictService.exportData(response);
    }

    @ApiOperation("导入数据")
    @PostMapping("/importData")
    public void importData(MultipartFile file){
        dictService.importData(file);
    }

    /**
     * 提供两个api接口，如省市区不需要上级编码，医院等级需要上级编码
     * @param dictCode
     * @param value
     * @return
     */
    @ApiOperation("获取数据字典名称")
    @GetMapping(value = "/getName/{dictCode}/{value}")
    public String getName(@PathVariable String dictCode,
                          @PathVariable String value){
        return dictService.getNameByDictCodeAndValue(dictCode,value);
    }

    @ApiOperation("获取数据字典名称")
    @GetMapping(value = "/getName/{value}")
    public String getName(@PathVariable String value){
        return dictService.getNameByDictCodeAndValue(null,value);
    }

    @ApiOperation(value = "根据字典编码查询子数据")
    @GetMapping(value = "/findByDictCode/{dictCode}")
    public Result<List<Dict>> findByDictCode(@PathVariable String dictCode) {
        List<Dict> list = dictService.findByDictCode(dictCode);
        return Result.ok(list);
    }

}
